import type { PageResponse } from './warehouse';

/**
 * 库存交易相关API
 */
import type { InventoryApi } from '#/models/inventory/inventory';

import { requestClient } from '#/api/request';

/**
 * 交易记录查询参数接口
 */
export interface TransactionQueryParams {
  page?: number;
  pageSize?: number;
  product_id?: number;
  from_warehouse_id?: number;
  to_warehouse_id?: number;
  transaction_type?: string;
  date_from?: string;
  date_to?: string;
  reference_document?: string;
  [key: string]: any;
}

/**
 * 创建交易参数接口
 */
export interface CreateTransactionParams {
  transaction_type: string;
  product_id: number;
  from_warehouse_id?: number;
  from_location_id?: number;
  to_warehouse_id?: number;
  to_location_id?: number;
  quantity: number;
  unit_cost?: number;
  reference_document?: string;
  reference_type?: string;
  transaction_reason?: string;
  lot_number?: string;
  serial_number?: string;
  remark?: string;
}

/**
 * 获取交易记录列表
 * @param params - 查询参数，支持分页、过滤和排序
 * @returns 分页的交易记录列表数据
 */
export const getTransactionList = (
  params?: TransactionQueryParams,
): Promise<PageResponse<InventoryApi.InventoryTransaction>> => {
  return requestClient.get('/inventory/transaction/', { params });
};

/**
 * 获取单个交易记录详情
 * @param transactionId - 交易记录ID
 * @returns 交易记录详情
 */
export const getTransaction = (
  transactionId: number,
): Promise<InventoryApi.InventoryTransaction> =>
  requestClient.get(`/inventory/transaction/${transactionId}/`);

/**
 * 创建交易记录
 * @param data - 交易记录数据
 * @returns 创建成功的交易记录信息
 */
export const createTransaction = (
  data: CreateTransactionParams,
): Promise<{
  inventory: InventoryApi.Inventory[];
  transaction: InventoryApi.InventoryTransaction;
}> => {
  return requestClient.post('/inventory/transaction/create-transaction/', data);
};

/**
 * 删除交易记录
 * @param transactionId - 交易记录ID
 * @returns 删除结果
 */
export const deleteTransaction = (transactionId: number): Promise<any> => {
  return requestClient.delete(`/inventory/transaction/${transactionId}/`);
};

/**
 * 导出交易记录
 * @param params - 导出参数
 * @returns 导出结果
 */
export const exportTransactions = (
  params?: TransactionQueryParams,
): Promise<Blob> => {
  return requestClient.get('/inventory/transaction/export/', {
    params,
    responseType: 'blob',
  });
};
